package com.cbank.controller.wap;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.InetAddress;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Country;
import com.maxmind.geoip2.record.Location;
import com.maxmind.geoip2.record.Postal;
import com.maxmind.geoip2.record.Subdivision;

public class Ip2City {

	private static FileReader fileReader;
	private static FileWriter fileWriter;
	static Map<String, Integer> map = new TreeMap<>();
	static int countError = 0;

	public static void main(String[] args) {
		// 创建 GeoLite2 数据库
		File database = new File("/Users/mac/workspace/GeoLite2-City.mmdb");
		File ips = new File("/Users/mac/workspace/ips100009");
		File iptj = new File("/Users/mac/workspace/ip统计100009");
		try {
			fileReader = new FileReader(ips);
			fileWriter = new FileWriter(iptj);
			BufferedWriter writer = new BufferedWriter(fileWriter);
			BufferedReader bufferedReader = new BufferedReader(fileReader);
			// 读取数据库内容
			DatabaseReader reader;
			reader = new DatabaseReader.Builder(database).build();
			String s = "";
			while ((s = bufferedReader.readLine()) != null) {
				try {
					InetAddress ipAddress = InetAddress.getByName(s);
					String cityName = getCityInfo(reader, ipAddress);
					if (map.containsKey(cityName)) {
						int countCity = map.get(cityName);
						map.put(cityName, ++countCity);
					} else {
						map.put(cityName, 0);
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			System.out.println("-------------------------------结束-------------------------------------");
			Set<String> keySet = map.keySet();
			
			for (String key : keySet) {
				int count = map.get(key);
				System.out.println(key+":"+count);
				writer.write(key+":"+count);
				writer.newLine();
			}
			writer.flush();
			writer.close();
			reader.close();
			// 获取查询结果
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static String getCityInfo(DatabaseReader reader, InetAddress ipAddress) throws IOException {
		CityResponse response;
		try {
			response = reader.city(ipAddress);

			// 获取国家信息
			Country country = response.getCountry();
			System.out.println(country.getIsoCode()); // 'CN'
			System.out.println(country.getName()); // 'China'
			System.out.println(country.getNames().get("zh-CN")); // '中国'

			// 获取省份
			Subdivision subdivision = response.getMostSpecificSubdivision();
			System.out.println(subdivision.getName()); // 'Guangxi Zhuangzu
														// Zizhiqu'
			System.out.println(subdivision.getIsoCode()); // '45'
			System.out.println(subdivision.getNames().get("zh-CN")); // '广西壮族自治区'

			// 获取城市
			City city = response.getCity();
			System.out.println(city.getName()); // 'Nanning'
			Postal postal = response.getPostal();
			System.out.println(postal.getCode()); // 'null'
			System.out.println(city.getNames().get("zh-CN")); // '南宁'
			Location location = response.getLocation();
			System.out.println(location.getLatitude()); // 22.8167
			System.out.println(location.getLongitude()); // 108.3167
			String cityname = country.getNames().get("zh-CN")+"-"+subdivision.getNames().get("zh-CN")+"-"+city.getNames().get("zh-CN");
			return cityname;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "-------错误------:" + countError++;
	}

}
